/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.actions.base;

import com.android1.amarena2d.nodes.behavior.ActionTarget;

public class Sequence extends IntervalAction {

    private FiniteTimeAction[] actions;
    private float split;
    private int last;


    public static Sequence $(FiniteTimeAction action1, FiniteTimeAction... actions) {
        if (action1 == null || actions == null || actions.length < 1)
            throw new IllegalArgumentException("A Sequence should have at least two Actions.");

        FiniteTimeAction prev = action1;
        for (FiniteTimeAction now : actions) {
            prev = new Sequence(prev, now);
        }
        return (Sequence) prev;
    }

    protected Sequence(FiniteTimeAction one, FiniteTimeAction two) {
        super(one.getDuration() + two.getDuration());

        actions = new FiniteTimeAction[2];
        actions[0] = one;
        actions[1] = two;
    }

    @Override
    public void start(ActionTarget aTarget) {
        super.start(aTarget);
        split = actions[0].getDuration() / duration;
        last = -1;
    }

    public void stop() {
        for (Action action : actions)
            action.stop();
        super.stop();
    }



    @Override
    public void update(float time) {

        int found;
        float new_t;

        if (time >= split) {
            found = 1;
            if (split == 1)
                new_t = 1;
            else
                new_t = (time - split) / (1 - split);
        } else {
            found = 0;
            if (split != 0)
                new_t = time / split;
            else
                new_t = 1;
        }

        if (last == -1 && found == 1) {
            actions[0].start(target);
            actions[0].update(1.0f);
            actions[0].stop();
        }
        final Action foundAction = actions[found];
        if (last != found) {
            if (last != -1) {
                actions[last].update(1.0f);
                actions[last].stop();
                actions[last].complete(target);
            }
            foundAction.start(target);
        }
        foundAction.update(new_t);
        last = found;


    }

    @Override
    public Sequence reverse() {
        return Sequence.$(actions[1].reverse(), actions[0].reverse());
    }


    @Override
    public Sequence copy() {
        return Sequence.$(actions[0].copy(), actions[1].copy());
    }

}
